OrderWin = {};

OrderWin.initPdfView = function() {
	var orderView = Ti.UI.createView({
		top:0,
		bottom:OrderWin.toolbar.height
	});
	
	orderView.pdfView = Ti.UI.createWebView();
	orderView.add(orderView.pdfView);
	
	orderView.indView = Ti.UI.createView({
		height:'100%',
		width:'100%',
		backgroundColor:'gray'
	});
	orderView.add(orderView.indView);
	
	orderView.actInd = Ti.UI.createActivityIndicator({
		style:Ti.UI.iPhone.ActivityIndicatorStyle.BIG
	});
	orderView.indView.add(orderView.actInd);
	
	var message = Titanium.UI.createLabel({
		text:"Génération du PDF...",
		color:'#fff',
		width:'auto',
		height:'auto',
		font:{fontSize:20, fontWeight:'bold'},
		bottom:20
	});
	orderView.indView.add(message);
	
	orderView.actInd.show();
	orderView.pdfView.url = Services.orderReportUrl(OrderWin.orderNum);
	
	orderView.addEventListener('load', function(e) {
		orderView.indView.animate({opacity:0, duration:500}, function() {
			orderView.indView.hide();
		});
	});
	
	return orderView;
};

OrderWin.initToolbar = function(parentToolbar) {
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	
	var btCancel = Ti.UI.createButton({
		title:'Annuler'
	});
	btCancel.addEventListener('click', function(e) {
		OrderWin.win.close();
		parentToolbar.show();
	});
	
	var btValidate = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Valider'
	});
	
	var popoverCancel = Ti.UI.createButton({
		title:'Annuler'
	});
	popoverCancel.addEventListener('click', function(e) {
		popover.close();
	});
	
	var popoverValidate = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Valider'
	});
	popoverValidate.addEventListener('click', function(e) {
		var signature = getSignatureView.evalJS('getSignature()');
		OrderWin.orderView.indView.show();
		OrderWin.orderView.indView.opacity = 1;
		OrderWin.orderView.pdfView.url = Services.orderReportWithSignatureUrl(OrderWin.orderNum, signature);
		
		var btReturn = Ti.UI.createButton({
			style:Ti.UI.iPhone.SystemButtonStyle.DONE,
			title:'Retour'
		});
		btReturn.addEventListener('click', function(e) {
			OrderWin.win.close();
			parentToolbar.show();
		});
		toolbar.items = [flexSpace, btReturn, flexSpace];
		
		popover.close();
	});
	
	var popover = Titanium.UI.createWindow({
		title:'Signature',
		leftNavButton:popoverCancel,
		rightNavButton:popoverValidate
	});
	
	var getSignatureView;
	
	btValidate.addEventListener('click', function(e) {
		getSignatureView = Ti.UI.createWebView({
			url:'signature.html'
		});
		
		popover.add(getSignatureView);
		
		popover.open({
			modal:true,
			modalTransitionStyle:Ti.UI.iPhone.MODAL_TRANSITION_STYLE_COVER_VERTICAL,
			modalStyle:Ti.UI.iPhone.MODAL_PRESENTATION_CURRENT_CONTEXT
		});
	});
	
	var toolbar = Titanium.UI.createToolbar({
		items:[flexSpace, btCancel, flexSpace, btValidate, flexSpace],
		bottom:0,
		borderTop:true,
		borderBottom:false
	});
	
	return toolbar;
};

OrderWin.open = function(orderNum, parentToolbar) {
	parentToolbar.hide();
	
	OrderWin.orderNum = orderNum;
	
	var win = createWindow({
		title:'Signature'
	});
	OrderWin.win = win;
	
	OrderWin.toolbar = OrderWin.initToolbar(parentToolbar);
	OrderWin.orderView = OrderWin.initPdfView();
	
	win.add(OrderWin.orderView);
	win.add(OrderWin.toolbar);
	win.open({
		modal:true,
		modalTransitionStyle:Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
		modalStyle:Ti.UI.iPhone.MODAL_PRESENTATION_PAGESHEET
	});
};